package service

import (
	"context"
	__ "user_service/basic/proto"
	"user_service/model"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Register(_ context.Context, in *__.RegisterReq) (*__.RegisterResp, error) {

	var u model.User
	u.GetUserBy(in.Mobile)

	if u.Id != 0 {
		return &__.RegisterResp{
			Code: 10000,
			Msg:  "用户已存在",
		}, nil
	}

	u = model.User{
		Mobile:   in.Mobile,
		Password: in.Password,
	}

	u.Create()

	return &__.RegisterResp{
		Code: 200,
		Msg:  "注册成功",
	}, nil
}

func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	var u model.User
	u.GetUserBy(in.Mobile)

	if u.Id == 0 {
		return &__.LoginResp{
			Code: 10000,
			Msg:  "用户不存在",
		}, nil
	}

	if u.Password == in.Password {
		return &__.LoginResp{
			Code: 10000,
			Msg:  "用户不存在",
		}, nil
	}

	return &__.LoginResp{
		Code: 200,
		Msg:  "登录成功",
	}, nil
}

func (s *Server) ListHome(_ context.Context, in *__.ListHomeReq) (*__.ListHomeResp, error) {
	var v model.UserHome
	list, err := v.List()
	if err != nil {
		return nil, err
	}

	var home []*__.ListHome
	for _, m := range list {
		lis := &__.ListHome{
			Id:       int64(m.Id),
			Title:    m.Title,
			Describe: m.Describe,
			VideoUrl: m.VideoUrl,
		}
		home = append(home, lis)
	}

	return &__.ListHomeResp{
		List: home,
	}, nil
}

func (s *Server) AddVideo(_ context.Context, in *__.AddVideoReq) (*__.AddVideoResp, error) {
	var v model.Video
	v.GetVideoBy(in.Title)

	v = model.Video{
		Title:       in.Title,
		UserName:    in.UserName,
		Liked:       uint(in.Liked),
		Contents:    uint(in.Contents),
		Forwards:    uint(in.Forwards),
		VideoStatus: uint(in.VideoStatus),
	}

	v.Create()

	return &__.AddVideoResp{
		Code: 200,
		Msg:  "添加成功",
	}, nil
}

func (s *Server) ListVideo(_ context.Context, in *__.ListVideoReq) (*__.ListVideoResp, error) {
	var v model.Video
	list, err := v.List()
	if err != nil {
		return nil, err
	}

	var video []*__.ListVideo
	for _, m := range list {
		lis := &__.ListVideo{
			Id:          int64(m.Id),
			Title:       m.Title,
			UserName:    m.UserName,
			Liked:       int64(m.Liked),
			Contents:    int64(m.Contents),
			Forwards:    int64(m.Forwards),
			VideoStatus: int64(m.VideoStatus),
		}
		video = append(video, lis)
	}

	return &__.ListVideoResp{
		List: video,
	}, nil
}

func (s *Server) Del(_ context.Context, in *__.DelVideoReq) (*__.DelVideoResp, error) {
	var v model.Video

	v = model.Video{
		Id: uint(in.Id),
	}

	v.Del(uint(in.Id))

	return &__.DelVideoResp{
		Code: 200,
		Msg:  "删除成功",
	}, nil
}

func (s *Server) Info(_ context.Context, in *__.InfoReq) (*__.InfoResp, error) {
	var v model.Video

	v.Info(in.Title)

	return &__.InfoResp{
		Id:          int64(v.Id),
		Title:       v.Title,
		UserName:    v.UserName,
		Liked:       int64(v.Liked),
		Contents:    int64(v.Contents),
		Forwards:    int64(v.Forwards),
		VideoStatus: int64(v.VideoStatus),
	}, nil
}
